package com.liaoyifan.core.filter;

import com.liaoyifan.core.constant.Properties;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

public class AutoConfigImportFilter implements AutoConfigurationImportFilter, EnvironmentAware {

    private boolean mybatisFlexEnable = false;
    private boolean datasourceEnable = false;
    private boolean openApiEnable = false;
    private boolean nacosEnable = false;
    private boolean feignEnable = false;
    private boolean actuatorEnable = false;
    private boolean sentinelEnable = false;

    @Override
    public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata metadata) {
        boolean[] results = new boolean[autoConfigurationClasses.length];
        for (int i = 0; i < autoConfigurationClasses.length; i++) {
            results[i] = determine(autoConfigurationClasses[i]);
        }
        return results;
    }

    private boolean determine(String className) {
        if (className == null) {
            return true;
        }
        if (className.startsWith("com.mybatisflex") || className.startsWith("org.mybatis")) {
            return mybatisFlexEnable;
        }
        if (className.startsWith("org.springframework.boot.autoconfigure.jdbc")
                || className.startsWith("org.springframework.boot.autoconfigure.transaction")
                || className.startsWith("org.springframework.boot.autoconfigure.sql")) {
            return datasourceEnable;
        }
        if (className.startsWith("org.springdoc")) {
            return openApiEnable;
        }
        if (className.startsWith("com.alibaba.cloud.nacos")) {
            return nacosEnable;
        }
        if (className.startsWith("org.springframework.cloud.openfeign")
                || className.startsWith("com.alibaba.cloud.circuitbreaker.sentinel.feign")
                || className.startsWith("com.alibaba.cloud.sentinel.feign")
                || className.startsWith("org.springframework.cloud.client.loadbalancer")
                || className.startsWith("org.springframework.cloud.loadbalancer")) {
            return feignEnable;
        }
        if (className.startsWith("org.springframework.boot.actuate")) {
            return actuatorEnable;
        }
        if (className.startsWith("com.alibaba.cloud.circuitbreaker.sentinel")
                || className.startsWith("com.alibaba.cloud.sentinel")) {
            return sentinelEnable;
        }
        return true;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.mybatisFlexEnable =
                environment.getProperty(Properties.MYBATIS_FLEX_ENABLE, Boolean.class, false);
        this.datasourceEnable =
                environment.getProperty(Properties.DATASOURCE_ENABLE, Boolean.class, false);
        this.openApiEnable =
                environment.getProperty(Properties.OPEN_API_ENABLE, Boolean.class, false);
        this.nacosEnable = environment.getProperty(Properties.NACOS_ENABLE, Boolean.class, false);
        this.feignEnable = environment.getProperty(Properties.FEIGN_ENABLE, Boolean.class, false);
        this.actuatorEnable =
                environment.getProperty(Properties.ACTUATOR_ENABLE, Boolean.class, false);
        this.sentinelEnable =
                environment.getProperty(Properties.SENTINEL_ENABLE, Boolean.class, false);
    }
}
